from hep.aida import *

from java.awt import Color
from java.awt import Font
from java.lang import *
from java.util import *
from java.io import *

from jhplot.jadraw import *
from jhplot  import *

from fr.in2p3.ipnl.aidautilities import *
from fr.in2p3.ipnl.oflap.scint import *
from jscinttools.utilities.jhplotutilities import *

Locale.setDefault(Locale.ENGLISH)
System.getProperties().setProperty("hep.aida.BatchAnalysisFactory","hep.aida.ref.BatchAnalysisFactory")

showFigures = False
writeFigures = not showFigures

""" Define colors and fonts"""
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 20)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 20)

""" Define the Path and files names"""
aidaPathL = "/home/marc-antoine/Documents/BGO/QFin"
aidaFileL = "ScintillationAnalysis_BGO"
#aidaPathQ = "/home/marc-antoine/Documents/BGO/QFin/QueensData/queenstools"
#aidaFileQ = "ScintillationAnalysis_BGO_QFile"
outPath = "/home/marc-antoine/Dropbox/BGO"
afrL = AidaFileReader(aidaPathL, aidaFileL)
#afrQ = AidaFileReader(aidaPathQ, aidaFileQ)

temper = ["299","30","3"]
#numComp = ["2","1","3"]
labPosX = [0.28, 0.56, 0.84]
#histoSup = [60,200,200]

"""
###############################################################
# Plotting Part
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects"""
hist = []
spec = []
func = []
label = []
for i in range(len(temper)):
	hist.append(afrL.getHisto("/Spectra/Spectrum_" + temper[i] + ".0K"))
	hist[i].scale(1.0/hist[i].maxBinHeight())
	spec.append(H1D(hist[i]))
	#spec.append(H1D(afrL.getHisto("/Spectra/Spectrum_" + temper[i] + ".0K")))
	spec[i].title = temper[i] + " K"
	spec[i].style = "h"
	#spec[i].setSymbol(0)
	#spec[i].setSymbolSize(12)
	spec[i].color = colors[0]
	spec[i].penWidth = 2
	spec[i].errY = False
	spec[i].errX = False
	#spec[i].errColor = mainP1D.getColor()
	
	#func.append(F1D(afrL.getFunc("/Average_Events/Average_Event_Func_" + temper[i] + ".0K_" + numComp[i] + "Comp"), -100, funcSup[i]))
	#func[i].title = temper[i]+ " K"
	#func[i].points = 10000
	#func[i].color = colors[1]
	#func[i].penWidth = 1
	
	label.append(JaText(temper[i]+ " K", labPosX[i], 0.25, "NDC"))
	label[i].font = legendFont
	label[i].color = colors[0]
	
	

""" Create the Plotter"""
mainPlotter = HPlotJa("Spectra", 1200, 600, 3, 1, True)
#mainPlotter.setGTitle("Spectra of 511 keV &gamma; in BGO", titleFont)
mainPlotter.antiAlias = False
if showFigures:
	mainPlotter.visible(True)
else:
	mainPlotter.visible(False)
	
mainPlotter.cd(1,1)	
mainPlotter.setPad(1, 1, 0.08, 0.16, 0.28, 0.68)
mainPlotter.setRange(0, 55, 0, 1.1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, False)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = "Number of Photons"
mainPlotter.nameY = "Normalized Counts"

mainPlotter.cd(2,1)	
mainPlotter.setPad(2, 1, 0.36, 0.16, 0.28, 0.68)
mainPlotter.setRange(0, 250, 0, 1.1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, False)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""

mainPlotter.cd(3,1)	
mainPlotter.setPad(3, 1, 0.64, 0.16, 0.28, 0.68)
mainPlotter.setRange(0, 250, 0, 1.1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, False)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""



""" Plots all the JHep objects"""
mainPlotter.cd(1,1)
mainPlotter.draw(spec[0])
#mainPlotter.draw(func[0])
mainPlotter.draw(label[0])

mainPlotter.cd(2,1)
mainPlotter.draw(spec[1])
#mainPlotter.draw(func[1])
mainPlotter.draw(label[1])
#theAxes_12 = mainPlotter.getPad()
#theAxes_12.setLabelSpace(0, 0.0)
#theAxes_12.setLabelSpace(1, 0.0)
#HPlotJaUtil.setTickLabelOneToPower(1, theAxes_12)

mainPlotter.cd(3,1)
mainPlotter.draw(spec[2])
#mainPlotter.draw(func[2])
mainPlotter.draw(label[2])
#theAxes_13 = mainPlotter.getPad()
#theAxes_13.setLabelSpace(0, 0.0)
#theAxes_13.setLabelSpace(1, 0.0)
#HPlotJaUtil.setTickLabelOneToPower(1, theAxes_13)


""" Modifies the JaAxes and the axes labels"""
mainPlotter.cd(1,1)
theAxes_11 = mainPlotter.getPad()
theAxes_11.setLabelSpace(0, 0.0)
theAxes_11.setTicksSize(0, theAxes_11.getTicksSize(0)/2.0)
theAxes_11.setSubTicksSize(0, theAxes_11.getSubTicksSize(0)/2.0)
theAxes_11.setLabelSpace(1, 0.0)
mainPlotter.addToBack(theAxes_11)
#HPlotJaUtil.setTickLabelOneToPower(1, theAxes_11)
labX_11 = mainPlotter.getLabelX()[0][0]
labX_11.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX_11, mainPlotter)
labY_11 = mainPlotter.getLabelY()[0][0]
labY_11.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY_11, mainPlotter, -0.015)

mainPlotter.cd(2,1)
theAxes_21 = mainPlotter.getPad()
theAxes_21.setLabelSpace(0, 0.0)
theAxes_21.setTicksSize(0, theAxes_21.getTicksSize(0)/2.0)
theAxes_21.setSubTicksSize(0, theAxes_21.getSubTicksSize(0)/2.0)
HPlotJaUtil.removeTickLabel(1, theAxes_21)
#theAxes_21.setLabelSpace(1, 0.0)
#HPlotJaUtil.setTickLabelOneToPower(1, theAxes_21)
#labX_21 = mainPlotter.getLabelX()[1][0]
#labX_21.setFont(axisLabelFont)
##HPlotJaUtil.setLabelLocationAtCenter(0, labX_21, mainPlotter)
#labY_21 = mainPlotter.getLabelY()[1][0]
#labY_21.setFont(axisLabelFont)
##HPlotJaUtil.setLabelLocationAtCenter(1, labY_21, mainPlotter)
#
mainPlotter.cd(3,1)
theAxes_31 = mainPlotter.getPad()
theAxes_31.setLabelSpace(0, 0.0)
theAxes_31.setTicksSize(0, theAxes_31.getTicksSize(0)/2.0)
theAxes_31.setSubTicksSize(0, theAxes_31.getSubTicksSize(0)/2.0)
HPlotJaUtil.removeTickLabel(1, theAxes_31)
#theAxes_31.setLabelSpace(1, 0.0)
#HPlotJaUtil.setTickLabelOneToPower(1, theAxes_31)
#labX_31 = mainPlotter.getLabelX()[2][0]
#labX_31.setFont(axisLabelFont)
#HPlotJaUtil.setLabelLocationAtCenter(0, labX_31, mainPlotter)
#labY_31 = mainPlotter.getLabelY()[2][0]
#labY_31.setFont(axisLabelFont)
#HPlotJaUtil.setLabelLocationAtCenter(1, labY_31, mainPlotter)



globTitle = JaText()
globTitle.text = "Spectra of 511 keV &gamma; in BGO"
globTitle.font = titleFont
mainPlotter.draw(globTitle)
HPlotJaUtil.setLabelLocationAtCenter(0, globTitle, mainPlotter, 0.9)

#HPlotJaUtil.setLabelLocationAtCenter(0, labX_11, mainPlotter)
#HPlotJaUtil.setLabelLocationAtCenter(1, labY_11, mainPlotter)
mainPlotter.update()

""" Saves or not the plot"""
if writeFigures:
	for i in range(len(figType)):
		outFileName = "specMultiV2" + figType[i]
		mainPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
mainPlotter.update()
print("That's all folks !")

